package com.minispider.util;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

import java.net.URL;
import java.net.HttpURLConnection;

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;

import cn.com.soosoo.robot.http.HttpConstants;
import cn.com.soosoo.robot.http.HttpDoc;
import cn.com.soosoo.robot.http.HttpTool;

public class SimpleHttpClient {

	private String url = null;
	 
	/*private String proxyUserName = null;
	private String proxyPassword = null;
	 
	private String proxyHost = null;
	private int proxyPort = 80;*/
	 	 
	private String charsetEncoding = Constants.CHARSET_ENCODING_DEFAULT;
	 
	public SimpleHttpClient(String url) throws Exception
	{
		init(url, null);
	}
	
	public SimpleHttpClient(String url, String charsetEncoding) throws Exception
	{
		init(url, charsetEncoding);
	}
	
	private void init(String url, String charsetEncoding) throws Exception
	{
		if (url == null)
		{
			throw new Exception("Class SimpleHttpClient : the url param is null.");
		}
		this.url = url.trim();
		
		if (charsetEncoding != null && charsetEncoding.trim().length() != 0)
		{
			this.charsetEncoding = charsetEncoding.trim();
		}
		
	}
	
	public void setProxy(String proxyHost, int proxyPort, final String proxyUserName, final String proxyPassword) 
	{
		
		Authenticator.setDefault(new Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() 
				{
					return new PasswordAuthentication(proxyUserName, new String(proxyPassword).toCharArray());
				}
			}
		);

		System.setProperty("http.proxyType", "4");
		System.setProperty("http.proxyHost", proxyHost);
		System.setProperty("http.proxyPort", Integer.toString(proxyPort));
		System.setProperty("http.proxySet", "true");

	}

	public StringBuffer getHttpServerPageContent(String ref) throws Exception
	{
		return getHttpServerPageContent(null, null, ref);
	}
	
	public StringBuffer getHttpServerPageContent(String url, String charsetEncoding, String ref) throws Exception
	{
		String _url = this.url;
		
		String chstEncoding = this.charsetEncoding;
		
		if (url != null && url.trim().length() != 0)
		{
			_url = url.trim();
		}
		
		if (charsetEncoding != null && charsetEncoding.trim().length() != 0)
		{
			chstEncoding = charsetEncoding.trim();
		}
			
		URL s = null;
		HttpURLConnection conn = null;

		BufferedReader reader = null;
		
		StringBuffer sb = new StringBuffer("");

		try {
			
			s = new URL(_url);
		
			conn = (HttpURLConnection) s.openConnection();
			
			conn.addRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA; .NET CLR 1.1.4322)");
			conn.addRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/QVOD, application/QVOD, application/vnd.ms-excel, application/msword, */*");
			//conn.addRequestProperty("Accept-Encoding", "gzip, deflate");
			conn.addRequestProperty("Referer", ref);
		
    		conn.connect();
			
			InputStream is = conn.getInputStream();
			
			//the default charset's encoding is utf-8.
			//chstEncoding : here is the http Server's page charset Encoding.
			reader = new BufferedReader(new InputStreamReader(is,  chstEncoding));
			
			String line = null;
			
			while ((line = reader.readLine()) != null) 
			{
	 			sb.append(line);
	 			sb.append(Constants.DOS_TERMINATORS);
			}
			
		} catch (Exception e) {
			throw e;
		}
		finally
		{
			try 
			{
				reader.close();	
				conn.disconnect();				
			} 
			catch (Exception e) 
			{
				throw e;
			}
			
		}
		
		return sb;
	}
	
	public static void main(String[] args) throws Exception {

		if (true) {
			
			for (int i = 0; i < 100; i++) {
				System.out.println(i + " ##############################");
				
				HttpTool ht = new HttpTool();
				ht.setAgentName("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA; .NET CLR 1.1.4322)");
				
				HttpDoc hd = null;
				
				int redo = 0;
				do {
					try {
						
						 hd = ht.retrieveDocument(
							new URL("http://www.tianya.cn/new/publicforum/articleslist.asp?pageNO=2&strItem=develop&Part=0&NextArticle=2010%2D5%2D16+20%3A05%3A56&strSubItem=&strSubItem2="), 
									HttpConstants.GET, "");
		
						redo = 0;
					} catch (Throwable t) {
						redo++;
						t.printStackTrace();
					}
				} while ( redo > 0 && redo < 30);
				
				System.out.println(hd.getContent().length);
				
				System.out.println(new String(hd.getContent(), "gb2312"));
			
			}
			
			
			
			
			return;
		}
		
		for (int i=0; i < 1; i ++) {
			
			System.out.print(i + "##################");

		SimpleHttpClient shc = new SimpleHttpClient(
				"http://www.tianya.cn/new/publicforum/articleslist.asp?pageNO=2&strItem=develop&Part=0&NextArticle=2010%2D5%2D14+15%3A05%3A18&strSubItem=&strSubItem2=", "gb2312");
		
		//shc.setProxy();
		
		StringBuffer content = shc.getHttpServerPageContent(
				"http://www.tianya.cn/publicforum/articleslist/0/water.shtml");

		System.out.print(content);
		
		}		
		/*
	    FileOperation fo = new FileOperation("E:/codes/java/workspace/test/www.163.com.txt", "utf-8");

		fo.writeto(content.toString());
		*/ 		
	}

}